package com.jxpowernode.bitcon.request;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Lixuewei
 * @Email:1410511702@qq.com
 * @Desc: 该类用于封装网络请求的代码，用于发起网络请求，并将响应数据进行返回
 * @Date: 2021/12/6 21:54
 */
public class HttpUtils {

    /**
     * 该函数用于实现get请求，并将返回结果返回
     *
     * @param path 请求得路径
     * @return 请求得结果
     */
    public static String get(String path) {
        InputStream inputStream = null;
        BufferedReader reader = null;
        InputStreamReader inputStreamReader = null;
        String line = ""; //开始得空集
        String result = ""; //得到累计得结果
        try {
            URL url = new URL(path);
            URLConnection con = url.openConnection();
            //进行连接
            con.connect();
            inputStream = con.getInputStream();
            //字符流，按行读出；
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            while ((line = reader.readLine()) != null) {
                result = result + line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return result;
    }

    /**
     * 该函数用于实现post请求，并将返回结果返回
     *
     * @param path 请求得路径
     * @param map  请求头设置信息
     * @param data 请求数据
     * @return 请求得结果
     */

    public static String post(String path, Map<String, String> map, byte[] data) {
        String result = "";//表示最后要返回的数据
        OutputStream outputStream = null;
        BufferedReader reader = null;
        String line = ""; //开始得空集
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置可以发送和接收数据
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //设置请求类型
            conn.setRequestMethod("POST");
            //设置请求头的属性
            if (!map.isEmpty()) {
                //将map中的数据遍历
//               for (String key : map.keySet() ){
//                   String value = map.get(key);
//                   conn.setRequestProperty(key,value);
//               }
                Set<String> keys = map.keySet();
                for (String key : keys) {
                    String value = map.get(key);
                    conn.setRequestProperty(key, value);
                }
            }
            conn.connect();
            //把请求数据发送到请求的服务端
            outputStream = conn.getOutputStream();
            outputStream.write(data);
            outputStream.flush();
            //等待接收返回的数据
           reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
                result = result + line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return result;
    }

}
